#include "FileReaderImp.h"
using namespace std;


FileReaderImp::FileReaderImp(void)
{
	_readByteCount = 0;
}


FileReaderImp::~FileReaderImp(void)
{
}

bool FileReaderImp::OpenFile(string file)
{
	_filename.assign(file);
	_file.open(_filename.c_str(), std::ios::in | std::ios::binary);
	return true;
}

void FileReaderImp::CloseFile()
{
	_file.close();
}

void FileReaderImp::ResetFile()
{
	if(_file.is_open())
		_file.close();
	_file.open(_filename.c_str(), std::ios::in | std::ios::binary);
	_readByteCount = 0;
}

bool FileReaderImp::Eof()
{
	return _file.eof();
}

bool FileReaderImp::Good()
{
	return _file.good();
}

BYTE FileReaderImp::ReadByte()
{
	BYTE data;
	_file.read((char*)(&data), 1);
	++_readByteCount;
	return data;
}

string FileReaderImp::ReadString()
{
	string ret;
	char ch;
	do
	{
		_file.read(&ch, 1);
		if(ch) 
			ret.push_back(ch);
	} while (ch != 0);
	_readByteCount += ret.size();
	return ret;
}

int FileReaderImp::ReadInt()
{
	char data[4];
	_file.read(data, sizeof(data));
	int ret = 0xffffffff;
	ret &= (data[3]<<24) | 0x00ffffff;
	ret &= (data[2]<<16) | 0xff00ffff;
	ret &= (data[1]<<8) | 0xffff00ff;
	ret &= (data[0]) | 0xffffff00;
	_readByteCount += 4;
	return ret;
}

int FileReaderImp::GetInFileBytes()
{
	return _readByteCount;
}
